package org.springblade.common.tool;

import org.springblade.common.constant.*;
import org.springblade.modules.monitor.dto.DiskDTO;
import org.springblade.modules.monitor.dto.DiskMetadata;
import org.springblade.modules.monitor.mapper.WorkOrderMapper;
import org.springblade.modules.monitor.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 服务器监控
 */

public class ServerMonitorUtil {


	public static ServerDetailVO transferServer(ServerDetailVO server, List<Map<String, Object>> strValues, List<Map<String, Object>> intValues, List<Map<String, Object>> values) {
		strValues.forEach(x->{
			String key_ = x.get("key_").toString();
			if(key_.equals("system.uname")){
				String systemName = x.get("value").toString();
				if(systemName.toUpperCase().contains(OperatingSystemEnum.WINDOWS.name())){
					server.setOperatingSystem(OperatingSystemEnum.WINDOWS.name());
				}else {
					server.setOperatingSystem(OperatingSystemEnum.LINUX.name());
				}
				server.setSystemDesc(systemName);
			}
			//system.sw.os，为对Linux 的描述
			if(key_.equals("system.sw.os")){
				server.setSystemDesc(x.get("value").toString());
			}
		});

		//获取CPU 信息 （CPU 利用率、核心数[核心利用率]）
		//获取Memory 信息(size ,available,pavailable)
		//获取 磁盘信息(size)
		CpuVO cpuVO = new CpuVO();
		MemoryVO memoryVO = new MemoryVO();
		NetworkVO networkVO = new NetworkVO();

		Map<String,Object> temp = new HashMap<>();
		intValues.forEach(x->{
			String key_ = x.get("key_").toString();
			//内存大小
			if(key_.equals("vm.memory.size[total]")){
				Long size = Long.valueOf(x.get("value").toString());
				DiskMetadata printSize = CommonUtil.getPrintSize(size,null);
				memoryVO.setMemoryTotal(size);
			}

			//磁盘信息
			if(key_.contains("vfs.fs.size[")){
				temp.put(key_, Long.valueOf(x.get("value").toString()));
			}

			/**
			 * NetWork
			 */
			if(key_.contains("net.if.in[")&&(!key_.contains(","))&&(StringUtils.isEmpty(networkVO.getReceivedItem()))){
				Long received = Long.valueOf(x.get("value").toString());
				networkVO.setReceived(received);
				networkVO.setReceivedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setReceivedClock(Long.valueOf(x.get("clock").toString()));
			}

			if(key_.contains("net.if.out[")&&(!key_.contains(","))&&(StringUtils.isEmpty(networkVO.getTransmittedItem()))){
				Long transmitted = Long.valueOf(x.get("value").toString());
				networkVO.setTransmitted(transmitted);
				networkVO.setTransmittedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setTransmittedClock(Long.valueOf(x.get("clock").toString()));
			}



		});
		List<DiskDTO> diskDTOS = CommonUtil.transferVFSInfo(temp,null);

		Map<String,Object> tempPercent = new HashMap<>();
		values.forEach(x->{
			String key_ = x.get("key_").toString();
			//CPU使用率
			if(key_.equals("system.cpu.util")){
				Double cpuUsage = Double.valueOf(x.get("value").toString());
				Double doubleCpuUsage = CommonUtil.fun2(cpuUsage);
				cpuVO.setCpuUsage(doubleCpuUsage);
				cpuVO.setCpuItem(Long.valueOf(x.get("itemid").toString()));
				cpuVO.setClock(Long.valueOf(x.get("clock").toString()));
			}
			//内存使用率
			if(key_.equals("vm.memory.size[pavailable]")||key_.equals("vm.memory.util")){
				Double memUsage = Double.valueOf(x.get("value").toString());
				Double doubleMemUsage = CommonUtil.fun2(memUsage);
				memoryVO.setMemoryUsage(doubleMemUsage);
				memoryVO.setMemoryItem(Long.valueOf(x.get("itemid").toString()));
				Double memUsed = memoryVO.getMemoryTotal() * memoryVO.getMemoryUsage() / 100;
				memoryVO.setMemoryUsed(memUsed.longValue());
				memoryVO.setClock(Long.valueOf(x.get("clock").toString()));
			}

			//磁盘使用率
			if(key_.contains("vfs.fs.size[")){
				tempPercent.put(key_, Double.valueOf(x.get("value").toString()));
			}
		});

		diskDTOS = CommonUtil.transferVFSInfo(tempPercent,diskDTOS);
		server.setDisks(diskDTOS);
		server.setCpuVO(cpuVO);
		server.setMemoryVO(memoryVO);
		server.setNetworkVO(networkVO);
		//获取指标列表（从上述数据抽取）
		//获取指标列表
		List<ServerQuota> serverQuotas = new ArrayList<>();

		//计算CPU使用率
		ServerQuota cpuQuota = new ServerQuota();
		cpuQuota.setName(QuotaEnum.CPU_USAGE.getQuotaName());
		cpuQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(cpuVO.getClock()));
		cpuQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		//TODO 制定策略 根据CPU使用率 决定状态
		cpuQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		Double pCPu = CommonUtil.fun2(Double.valueOf(cpuVO.getCpuUsage()));
		cpuQuota.setCurrentValue(pCPu+"%");
		serverQuotas.add(cpuQuota);

		//内存总量
		ServerQuota memTotalQuota = new ServerQuota();
		memTotalQuota.setName(QuotaEnum.MEMORY_TOTAL.getQuotaName());
		memTotalQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memTotalQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memTotalQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		DiskMetadata printSizeMemTotal = CommonUtil.getPrintSize(memoryVO.getMemoryTotal(), null);
		memTotalQuota.setCurrentValue(printSizeMemTotal.getSize()+printSizeMemTotal.getUnit());
		serverQuotas.add(memTotalQuota);

		//已使用内存
		ServerQuota memUsedQuota = new ServerQuota();
		memUsedQuota.setName(QuotaEnum.MEMORY_USED.getQuotaName());
		memUsedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		Double memUsed = memoryVO.getMemoryTotal() * memoryVO.getMemoryUsage() / 100;
		DiskMetadata printSizeMemUsed = CommonUtil.getPrintSize(Long.valueOf(memUsed.longValue()), null);
		memUsedQuota.setCurrentValue(printSizeMemUsed.getSize()+printSizeMemUsed.getUnit());
		serverQuotas.add(memUsedQuota);

		//内存使用率
		ServerQuota memUsageQuota = new ServerQuota();
		memUsageQuota.setName(QuotaEnum.MEMORY_USAGE.getQuotaName());
		memUsageQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsageQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsageQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		memUsageQuota.setCurrentValue(CommonUtil.fun2(memoryVO.getMemoryUsage())+"%");
		serverQuotas.add(memUsageQuota);


		//网络接收速度
		ServerQuota receivedQuota = new ServerQuota();
		receivedQuota.setName(QuotaEnum.RECEIVED_SPEED.getQuotaName());
		receivedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(networkVO.getReceivedClock()));
		receivedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		receivedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		receivedQuota.setCurrentValue(networkVO.getReceived()/1024.0+"KBps");
		serverQuotas.add(receivedQuota);

		//网络发送速度
		ServerQuota transmittedQuota = new ServerQuota();
		transmittedQuota.setName(QuotaEnum.TRANSMITTED_SPEED.getQuotaName());
		transmittedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(networkVO.getTransmittedClock()));
		transmittedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		transmittedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		transmittedQuota.setCurrentValue(networkVO.getTransmitted()/1024.0+"KBps");
		serverQuotas.add(transmittedQuota);
		server.setServerQuotas(serverQuotas);
		return server;
	}


	//TODO 转换EXSI 信息 START
	public static ExsiVO getEXSIdetail(ExsiVO exsiVO, List<Map<String, Object>> strValues, List<Map<String, Object>> intValues, List<Map<String, Object>> values) {
		CpuVO cpuVO = new CpuVO();
		MemoryVO memoryVO = new MemoryVO();
		NetworkVO networkVO = new NetworkVO();

		strValues.forEach(x->{
			String key_ = x.get("key_").toString();

			if(key_.contains("vmware.hv.fullname[")){
				exsiVO.getExsiGeneralVO().setOperatingSystem(x.get("value").toString());
			}

			//model
			if(key_.contains("vmware.hv.hw.model[")){
				exsiVO.getExsiGeneralVO().setModel(x.get("value").toString());
			}

			//vendor
			if(key_.contains("vmware.hv.hw.vendor[")){
				exsiVO.getExsiGeneralVO().setVendor(x.get("value").toString());
			}

			//CPU model
			if(key_.contains("vmware.hv.hw.cpu.model[")){
				cpuVO.setModel(x.get("value").toString());
			}


			//cluster
			if(key_.contains("vmware.hv.cluster.name[")){
				exsiVO.getExsiGeneralVO().setClusterName(x.get("value").toString());
			}


		});

		//获取CPU 信息 （CPU 利用率、核心数[核心利用率]）
		//获取Memory 信息(size ,available,pavailable)
		//获取 磁盘信息(size)

//		Map<String,Object> temp = new HashMap<>();
		List<DatastoreVO> datastoreVOS = new ArrayList<>();
		intValues.forEach(x->{
			/**
			 * CPU信息
			 */
			String key_ = x.get("key_").toString();
			// CPU核数
			if(key_.contains("vmware.hv.hw.cpu.num[")){
				Integer cpuNums = Integer.valueOf(x.get("value").toString());
				cpuVO.setCoreNum(cpuNums);
			}

			if(key_.contains("vmware.hv.hw.cpu.freq[")){
				Long cpuFrequency = Long.valueOf(x.get("value").toString());
				cpuVO.setCoreFrequency(cpuFrequency.doubleValue());
			}

			//CPU threads
			if(key_.contains("vmware.hv.hw.cpu.threads[")){
				Integer cpuThreads = Integer.valueOf(x.get("value").toString());
				cpuVO.setThreadNum(cpuThreads);
			}

			//CPU usage
			if(key_.contains("vmware.hv.cpu.usage[")){
				Long cpuUsage = Long.valueOf(x.get("value").toString());
				cpuVO.setCpuUsed(cpuUsage.doubleValue());
				cpuVO.setCpuItem(Long.valueOf(x.get("itemid").toString()));
				cpuVO.setClock(Long.valueOf(x.get("clock").toString()));
			}

			/**
			 * 内存信息
			 */
			//Total memory
			if(key_.contains("vmware.hv.hw.memory[")){
				Long size = Long.valueOf(x.get("value").toString());
				memoryVO.setMemoryTotal(size);
			}

			//Used memory
			if(key_.contains("vmware.hv.memory.used[")){
				Long used = Long.valueOf(x.get("value").toString());
				memoryVO.setMemoryUsed(used);
				memoryVO.setMemoryItem(Long.valueOf(x.get("itemid").toString()));
				memoryVO.setClock(Long.valueOf(x.get("clock").toString()));
			}



			//DataStore信息
			if(key_.contains("vmware.hv.datastore.size[")){

				DatastoreVO datastoreVO = new DatastoreVO();
				datastoreVO.setDiskName(CommonUtil.splitKeytTargetCharacter(key_,",")[2]);
				datastoreVOS.add(datastoreVO);
			}

			/**
			 * NetWork
			 */
			if(key_.contains("vmware.hv.network.in[")){
				Long received = Long.valueOf(x.get("value").toString());
				networkVO.setReceived(received);
				networkVO.setReceivedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setReceivedClock(Long.valueOf(x.get("clock").toString()));
			}

			if(key_.contains("vmware.hv.network.out[")){
				Long transmitted = Long.valueOf(x.get("value").toString());
				networkVO.setTransmitted(transmitted);
				networkVO.setTransmittedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setTransmittedClock(Long.valueOf(x.get("clock").toString()));
			}

			/**
			 * Number of guest VMs
			 */
			if(key_.contains("vmware.hv.vm.num[")){
				Integer vmNums = Integer.valueOf(x.get("value").toString());
				exsiVO.getExsiGeneralVO().setVmNum(vmNums);
				exsiVO.getExsiGeneralVO().setVmNumClock(Long.valueOf(x.get("clock").toString()));
			}

		});

		intValues.forEach(x->{
			String key_ = x.get("key_").toString();
			datastoreVOS.forEach(y->{
				if(key_.contains("vmware.hv.datastore.size[")&&key_.contains(y.getDiskName())){
					y.setDiskTotal(Long.valueOf(x.get("value").toString()));
				}
			});
		});

		values.forEach(x->{
			String key_ = x.get("key_").toString();
			datastoreVOS.forEach(y->{
				if(key_.contains("vmware.hv.datastore.size[")&&key_.contains(y.getDiskName())){
					y.setDiskFreePercent(Double.valueOf(x.get("value").toString()));
				}
			});
		});

		exsiVO.setDatastores(datastoreVOS);
		//TODO 获取是否存在故障、故障类型、等级(关联blade_alert 进行查询)

		//获取指标列表
		List<ServerQuota> serverQuotas = new ArrayList<>();

		//计算CPU使用率
		ServerQuota cpuQuota = new ServerQuota();
		cpuQuota.setName(QuotaEnum.CPU_USAGE.getQuotaName());
		cpuQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(cpuVO.getClock()));
		cpuQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		//TODO 制定策略 根据CPU使用率 决定状态
		cpuQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		Double pCPu = CommonUtil.fun2(Double.valueOf(cpuVO.getCpuUsed() * 100 / cpuVO.getCoreFrequency() / cpuVO.getCoreNum()));
		cpuQuota.setCurrentValue(pCPu+"%");
		cpuVO.setCpuUsage(cpuVO.getCpuUsed()*100/cpuVO.getCoreFrequency()/cpuVO.getCoreNum());
		serverQuotas.add(cpuQuota);

		//内存总量
		ServerQuota memTotalQuota = new ServerQuota();
		memTotalQuota.setName(QuotaEnum.MEMORY_TOTAL.getQuotaName());
		memTotalQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memTotalQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memTotalQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		DiskMetadata printSizeMemTotal = CommonUtil.getPrintSize(memoryVO.getMemoryTotal(), null);
		memTotalQuota.setCurrentValue(printSizeMemTotal.getSize()+printSizeMemTotal.getUnit());
		serverQuotas.add(memTotalQuota);

		//已使用内存
		ServerQuota memUsedQuota = new ServerQuota();
		memUsedQuota.setName(QuotaEnum.MEMORY_USED.getQuotaName());
		memUsedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		DiskMetadata printSizeMemUsed = CommonUtil.getPrintSize(memoryVO.getMemoryUsed(), null);
		memUsedQuota.setCurrentValue(printSizeMemUsed.getSize()+printSizeMemUsed.getUnit());
		serverQuotas.add(memUsedQuota);

		//内存使用率
		ServerQuota memUsageQuota = new ServerQuota();
		memUsageQuota.setName(QuotaEnum.MEMORY_USAGE.getQuotaName());
		memUsageQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsageQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsageQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		memUsageQuota.setCurrentValue(CommonUtil.fun2(memoryVO.getMemoryUsed()*100.0/memoryVO.getMemoryTotal())+"%");
		memoryVO.setMemoryUsage(memoryVO.getMemoryUsed()*100.0/memoryVO.getMemoryTotal());
		serverQuotas.add(memUsageQuota);

		//虚拟机数量
		ServerQuota vmQuota = new ServerQuota();
		vmQuota.setName(QuotaEnum.VMNUMBER.getQuotaName());
		vmQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(exsiVO.getExsiGeneralVO().getVmNumClock()));
		vmQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		vmQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		vmQuota.setCurrentValue(exsiVO.getExsiGeneralVO().getVmNum().toString());
		serverQuotas.add(vmQuota);

		//网络接收速度
		ServerQuota receivedQuota = new ServerQuota();
		receivedQuota.setName(QuotaEnum.RECEIVED_SPEED.getQuotaName());
		receivedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(networkVO.getReceivedClock()));
		receivedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		receivedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		receivedQuota.setCurrentValue(networkVO.getReceived()/1024.0+"KBps");
		serverQuotas.add(receivedQuota);

		//网络发送速度
		ServerQuota transmittedQuota = new ServerQuota();
		transmittedQuota.setName(QuotaEnum.TRANSMITTED_SPEED.getQuotaName());
		transmittedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(networkVO.getTransmittedClock()));
		transmittedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		transmittedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		transmittedQuota.setCurrentValue(networkVO.getTransmitted()/1024.0+"KBps");
		serverQuotas.add(transmittedQuota);

		exsiVO.setCpuVO(cpuVO);
		exsiVO.setMemoryVO(memoryVO);
		exsiVO.setNetworkVO(networkVO);
		exsiVO.setServerQuotas(serverQuotas);
		exsiVO.setServerQuotas(serverQuotas);

		// TODO 从blade_alerts获取健康度

		exsiVO.getExsiGeneralVO().setHealthyValue(100);
		return exsiVO;
	}

	//转换EXSI 信息 END



	//TODO 转换VMMachine 信息 START
	public static VMMachineVO getVMMachinedetail(VMMachineVO vmMachineVO, List<Map<String, Object>> strValues, List<Map<String, Object>> intValues, List<Map<String, Object>> values) {
		VMCpuVO cpuVO = new VMCpuVO();
		MemoryVO memoryVO = new MemoryVO();
		NetworkVO networkVO = new NetworkVO();

		strValues.forEach(x->{
			String key_ = x.get("key_").toString();
			//Hypervisor name
			if(key_.contains("vmware.vm.hv.name[")){
				vmMachineVO.getVMMachineGeneralVO().setHypervisorName(x.get("value").toString());
			}

			//cluster
			if(key_.contains("vmware.vm.cluster.name[")){
				vmMachineVO.getVMMachineGeneralVO().setClusterName(x.get("value").toString());
			}
		});

		//获取CPU 信息 （CPU 利用率、核心数[核心利用率]）
		//获取Memory 信息(size ,available,pavailable)
		//获取 磁盘信息(size)

//		Map<String,Object> temp = new HashMap<>();
		List<DatastoreVO> datastoreVOS = new ArrayList<>();
		intValues.forEach(x->{
			/**
			 * CPU信息
			 */
			String key_ = x.get("key_").toString();
			// 虚拟CPU数量
			if(key_.contains("vmware.vm.cpu.num[")){
				Integer cpuNums = Integer.valueOf(x.get("value").toString());
				cpuVO.setCoreNum(cpuNums);
			}


			//CPU used
			if(key_.contains("vmware.vm.cpu.usage[")){
				Long cpuUsage = Long.valueOf(x.get("value").toString());
				cpuVO.setCpuUsed(cpuUsage.doubleValue());
				cpuVO.setCpuItem(Long.valueOf(x.get("itemid").toString()));
				cpuVO.setClock(Long.valueOf(x.get("clock").toString()));
			}

			/**
			 * 内存信息
			 */
			//Total memory
			if(key_.contains("vmware.vm.memory.size[")){
				Long size = Long.valueOf(x.get("value").toString());
				memoryVO.setMemoryTotal(size);
			}

			//Used memory
			if(key_.contains("vmware.vm.memory.size.usage.guest[")){
				Long used = Long.valueOf(x.get("value").toString());
				memoryVO.setMemoryUsed(used);
				memoryVO.setMemoryItem(Long.valueOf(x.get("itemid").toString()));
				memoryVO.setClock(Long.valueOf(x.get("clock").toString()));
			}


			//DataStore信息
			if(key_.contains("vmware.vm.vfs.fs.size[")){
				DatastoreVO datastoreVO = new DatastoreVO();
				String tempDiskName = CommonUtil.splitKeytTargetCharacter(key_, ",")[2];
				//判断是否已经存在
				boolean flag = false;
				for (DatastoreVO vo : datastoreVOS) {
					if(tempDiskName.equals(vo.getDiskName())){
						flag = true;
					}
				}

				if(!flag){
					datastoreVO.setDiskName(tempDiskName);
					datastoreVOS.add(datastoreVO);
				}
			}

			/**
			 * NetWork
			 */
			if(key_.contains("vmware.vm.net.if.in[")){
				Long received = Long.valueOf(x.get("value").toString());
				networkVO.setReceived(received);
				networkVO.setReceivedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setReceivedClock(Long.valueOf(x.get("clock").toString()));
			}

			if(key_.contains("vmware.vm.net.if.out[")){
				Long transmitted = Long.valueOf(x.get("value").toString());
				networkVO.setTransmitted(transmitted);
				networkVO.setTransmittedItem(Long.valueOf(x.get("itemid").toString()));
				networkVO.setTransmittedClock(Long.valueOf(x.get("clock").toString()));
			}

			/**
			 * Power state
			 */
			if(key_.contains("vmware.vm.powerstate[")){
				Integer powerState = Integer.valueOf(x.get("value").toString());
				vmMachineVO.getVMMachineGeneralVO().setPowerState(powerState);
				vmMachineVO.getVMMachineGeneralVO().setPowerStateClock(Long.valueOf(x.get("clock").toString()));
			}

		});

		intValues.forEach(x->{
			String key_ = x.get("key_").toString();
			datastoreVOS.forEach(y->{
				if(key_.contains("vmware.vm.vfs.fs.size[")&&key_.contains(y.getDiskName())){
					y.setDiskTotal(Long.valueOf(x.get("value").toString()));
				}
			});
		});

		values.forEach(x->{
			String key_ = x.get("key_").toString();
			datastoreVOS.forEach(y->{
				if(key_.contains("vmware.vm.vfs.fs.size[")&&key_.contains(y.getDiskName())){
					y.setDiskFreePercent(Double.valueOf(x.get("value").toString()));
				}
			});
		});

		vmMachineVO.setDatastores(datastoreVOS);
		//TODO 获取是否存在故障、故障类型、等级(关联blade_alert 进行查询)

		//获取指标列表

		//获取指标列表
		List<ServerQuota> serverQuotas = new ArrayList<>();

		//计算CPU使用
		ServerQuota cpuQuota = new ServerQuota();
		cpuQuota.setName(QuotaEnum.CPU_USED.getQuotaName());
		cpuQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(cpuVO.getClock()));
		cpuQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		//TODO 制定策略 根据CPU使用率 决定状态
		cpuQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		cpuQuota.setCurrentValue(cpuVO.getCpuUsed() / 1000000+"MHZ");
		serverQuotas.add(cpuQuota);

		//计算CPU使用率
		double intialCpuUsed =  cpuVO.getCpuUsed();
		double v = intialCpuUsed*100 / (cpuVO.getCoreNum() * (vmMachineVO.getVMMachineGeneralVO().getCoreFrequency()));
		cpuVO.setCpuUsage(CommonUtil.fun2(v));

		ServerQuota cpuUsageQuota = new ServerQuota();
		cpuUsageQuota.setName(QuotaEnum.CPU_USAGE.getQuotaName());
		cpuUsageQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(cpuVO.getClock()));
		cpuUsageQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		//TODO 制定策略 根据CPU使用率 决定状态
		cpuUsageQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		cpuUsageQuota.setCurrentValue(CommonUtil.fun2(cpuVO.getCpuUsage())+"%");
		serverQuotas.add(cpuUsageQuota);

		//内存总量
		ServerQuota memTotalQuota = new ServerQuota();
		memTotalQuota.setName(QuotaEnum.MEMORY_TOTAL.getQuotaName());
		memTotalQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memTotalQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memTotalQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		DiskMetadata printSizeMemTotal = CommonUtil.getPrintSize(memoryVO.getMemoryTotal(), null);
		memTotalQuota.setCurrentValue(printSizeMemTotal.getSize()+printSizeMemTotal.getUnit());
		serverQuotas.add(memTotalQuota);

		//已使用内存
		ServerQuota memUsedQuota = new ServerQuota();
		memUsedQuota.setName(QuotaEnum.MEMORY_USED.getQuotaName());
		memUsedQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		DiskMetadata printSizeMemUsed = CommonUtil.getPrintSize(memoryVO.getMemoryUsed(), null);
		memUsedQuota.setCurrentValue(printSizeMemUsed.getSize()+printSizeMemUsed.getUnit());
		serverQuotas.add(memUsedQuota);

		//内存使用率
		ServerQuota memUsageQuota = new ServerQuota();
		memUsageQuota.setName(QuotaEnum.MEMORY_USAGE.getQuotaName());
		memUsageQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(memoryVO.getClock()));
		memUsageQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		memUsageQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		memUsageQuota.setCurrentValue(CommonUtil.fun2(memoryVO.getMemoryUsed()*100.0/memoryVO.getMemoryTotal())+"%");
		memoryVO.setMemoryUsage(memoryVO.getMemoryUsed()*100.0/memoryVO.getMemoryTotal());
		serverQuotas.add(memUsageQuota);

		//电源状态
		ServerQuota powerStateQuota = new ServerQuota();
		powerStateQuota.setName(QuotaEnum.POWER_STATE.getQuotaName());
		powerStateQuota.setCollectionTime(CommonUtil.getTimeFromMillSecond(vmMachineVO.getVMMachineGeneralVO().getPowerStateClock()));
		powerStateQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		powerStateQuota.setCurrentValue(vmMachineVO.getVMMachineGeneralVO().getPowerState().toString());
		if(vmMachineVO.getVMMachineGeneralVO().getPowerState()==0){
			powerStateQuota.setStatus(QuotaStatusEnum.WARNING.getStatus());
			powerStateQuota.setCurrentValue("powerOff");
		}else {
			powerStateQuota.setStatus(QuotaStatusEnum.OK.getStatus());
			powerStateQuota.setCurrentValue("powerOn");
		}
		serverQuotas.add(powerStateQuota);
		//网络接收速度
		ServerQuota receivedQuota = new ServerQuota();
		receivedQuota.setName(QuotaEnum.RECEIVED_SPEED.getQuotaName());
		receivedQuota.setCollectionTime(networkVO.getReceivedClock()==null?null:CommonUtil.getTimeFromMillSecond(networkVO.getReceivedClock()));
		receivedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		receivedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		receivedQuota.setCurrentValue(networkVO.getReceived()==null?"0KBps":(networkVO.getReceived()/1024.0+"KBps"));
		serverQuotas.add(receivedQuota);

		//网络发送速度
		ServerQuota transmittedQuota = new ServerQuota();
		transmittedQuota.setName(QuotaEnum.TRANSMITTED_SPEED.getQuotaName());
		transmittedQuota.setCollectionTime(networkVO.getTransmittedClock()==null?null:CommonUtil.getTimeFromMillSecond(networkVO.getTransmittedClock()));
		transmittedQuota.setKind(QuotaKindEnum.PERFORMANCE.getName());
		transmittedQuota.setStatus(QuotaStatusEnum.OK.getStatus());
		transmittedQuota.setCurrentValue(networkVO.getTransmitted()==null?"0KBps":(networkVO.getTransmitted()/1024.0+"KBps"));
		serverQuotas.add(transmittedQuota);

		vmMachineVO.setCpuVO(cpuVO);
		vmMachineVO.setMemoryVO(memoryVO);
		vmMachineVO.setNetworkVO(networkVO);
		vmMachineVO.setServerQuotas(serverQuotas);

		// TODO 从blade_alerts获取健康度
		vmMachineVO.getVMMachineGeneralVO().setHealthyValue(100);

		return vmMachineVO;
	}

	//转换VMMachine 信息 END



	//TODO 转换VCenter 信息 START
	public static VCenterVO getVCenterdetail(VCenterVO vCenterVO, List<Map<String, Object>> strValues, List<Map<String, Object>> intValues, List<Map<String, Object>> values) {
		VMCpuVO cpuVO = new VMCpuVO();
		MemoryVO memoryVO = new MemoryVO();
		NetworkVO networkVO = new NetworkVO();

		strValues.forEach(x->{
			String key_ = x.get("key_").toString();
			//Full name
			if(key_.contains("vmware.fullname[")){
				vCenterVO.getVCenterGeneralVO().setOperatingSystem(x.get("value").toString());
			}
		});

		List<DatastoreVO> datastoreVOS = new ArrayList<>();
		List<VCenterClusterVO> vCenterClusterVOS = new ArrayList<>();
		intValues.forEach(x->{
			/**
			 * CPU信息
			 */
			String key_ = x.get("key_").toString();
			//DataStore信息
			if(key_.contains("vmware.datastore.size[")&&key_.endsWith("pfree]")){
				DatastoreVO datastoreVO = new DatastoreVO();
				datastoreVO.setDiskName(CommonUtil.splitKeytTargetCharacter(key_,",")[1]);
				datastoreVOS.add(datastoreVO);
			}

			/**
			 * cluster
			 */
			if(key_.contains("vmware.cluster.status[")){
				VCenterClusterVO vCenterClusterVO = new VCenterClusterVO();
				vCenterClusterVO.setClusterName(CommonUtil.splitKeytTargetCharacter(key_,",")[1]);
				vCenterClusterVOS.add(vCenterClusterVO);
			}
		});

		intValues.forEach(x->{
			String key_ = x.get("key_").toString();
			datastoreVOS.forEach(y->{
				if(key_.contains("vmware.datastore.size[")&&(!key_.contains("pfree"))){
					if(key_.contains(y.getDiskName())){
						y.setDiskTotal(Long.valueOf(x.get("value").toString()));
					}
				}
				if(key_.contains("vmware.datastore.size[")&&(key_.contains("pfree"))){
					if(key_.contains(y.getDiskName())){
						y.setDiskFreePercent(Long.valueOf(x.get("value").toString()).doubleValue());
					}
				}
			});
		});

		intValues.forEach(x->{
			String key_ = x.get("key_").toString();
			vCenterClusterVOS.forEach(y->{
				if(key_.contains("vmware.cluster.status[")&&(!key_.contains("pfree"))){
					if(CommonUtil.splitKeytTargetCharacter(key_,",")[1].equals(y.getClusterName())){
						y.setClusterStatus(Integer.valueOf(x.get("value").toString()));
					}
				}
			});
		});


		vCenterVO.setDatastores(datastoreVOS);
		//TODO 获取是否存在故障、故障类型、等级(关联blade_alert 进行查询)
		//获取指标列表（从上述数据抽取）
		vCenterVO.setVCenterClusters(vCenterClusterVOS);
		return vCenterVO;
	}

}
